package com.cjd.common.adapter

import androidx.recyclerview.widget.RecyclerView
import com.cjd.common.holder.BaseRecyclerHolder


/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/7/3
 * description
 */
abstract class BaseRecyclerArrayAdapter<T, VH : BaseRecyclerHolder> : RecyclerView.Adapter<VH>() {

    var currentItems: MutableList<T> = mutableListOf()

    fun add(data: T) {
        add(data, false)
    }

    fun add(data: T, animation: Boolean) {
        currentItems.add(data)
        if (animation) {
            notifyItemInserted(currentItems.size - 1)
        } else
            notifyDataSetChanged()
    }

    fun add(index: Int, data: T) {
        add(index, data, false)
    }

    fun add(index: Int, data: T, animation: Boolean) {
        currentItems.add(index, data)
        if (animation) {
            notifyItemInserted(index)
        } else
            notifyDataSetChanged()
    }

    fun remove(index: Int,animation: Boolean){
        currentItems.removeAt(index)
        if (animation)
            notifyItemRemoved(index)
        else
            notifyDataSetChanged()
    }

    open fun addAll(collection: Collection<T>?) {
        addAll(collection, false)
    }

    open fun addAll(collection: Collection<T>?, animation: Boolean) {
        if (collection != null) {
            currentItems.addAll(collection)
            if (animation)
                notifyItemRangeInserted(currentItems.size - collection.size, currentItems.size - 1)
            else
                notifyDataSetChanged()
        }
    }

    fun replaceAll(collection: Collection<T>) {
        currentItems.clear()
        addAll(collection)
    }

    fun getItem(position: Int): T {
        return currentItems[position]
    }

    override fun getItemCount(): Int {
        return currentItems.size
    }

    fun getAll(): MutableList<T> {
        return currentItems
    }
}